<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/4/27
 * Time: 17:39
 */

namespace app\common;


use think\Db;

class attr
{
    public static function  get_list($type,$display=false,$order=false)
    {
        if($display){
            $where['display'] = 1;
        }else{
            $where = '1=1';
        }
        $order = $order?'sort desc':'';
        $data = Db::name('attr')
            ->where('type',$type)
            ->where($where)
            ->order($order)
            ->order('id desc')
            ->select();
        return $data;
    }

    public static function add($type)
    {
        $data['name'] =  input('name');
        $data['type'] =$type;
        $data['sort'] = input('sort/d')?:0;
        $data['display'] = input('display/d')?:1;
        $data['addtime'] = $data['modtime'] = time();
        $a = Db::name('attr')->insert($data);
        return $a;
    }

    public static function update()
    {
        $field = input('field');
        $value = input('value');
        $id = input('id/d');
        if($value == ''){
            echo api_return(0,'不能为空');
            exit;
        }
        if($field == 'sort' || $field == 'display'){
            $value = intval($value);
        }
        $data[$field] = $value;
        $data['modtime'] = time();
        $a = Db::name('attr')->where('id',$id)->update($data);
        return $a;
    }

    public static function del()
    {
        $id = input('id/d');
        $a = Db::name('attr')->where('id',$id)->delete();
        return $a;
    }
}